package com.kusiri.kframework.salesforce.ui.component.sobject
{
	import com.kusiri.kframework.salesforce.model.ISObject;
	import com.kusiri.kframework.salesforce.ui.SObjectForm;
	import com.kusiri.kframework.ui.KfUiComponent;
	import com.salesforce.results.DescribeLayoutResult;
	
	import mx.core.IFlexDisplayObject;

	public class KfSObjectView extends KfUiComponent implements IKfSObjectView
	{
		private var _sobject : ISObject;
		
		public function get valueObject() : ISObject
		{
			return _sobject;
		}
		
		public function set valueObject( sobject : ISObject) : void
		{
			_sobject = sobject;
		}
		 
		/**
		 * Whether or not the initial state is edit
		 */
		public var openInEdit : Boolean;
		
		private var self : KfSObjectView;
		
		/**
		 * This is is the layout
		 */
		private var uiComponent : SObjectForm; 
		
		public function KfSObjectView()
		{
			super();
			self = this;
		}
	
		/**
		 * Get the Flex UI Component
		 */
		override public function get __uiComponent() : IFlexDisplayObject
		{
			return uiComponent;
		}

		/**
		 * Tell the UI Component to load its data.
		 */
		override public function __initializeComponent(onSuccess : Function, onFail : Function) : void
		{
			valueObject.__ensureFullyLoaded(
				function() : void
				{
					__name = valueObject.__type;
					valueObject.__describeLayout(
						function(layout:DescribeLayoutResult) : void
						{
							uiComponent = new SObjectForm(layout, valueObject, null,  null, null, null, false, openInEdit);
	
							onSuccess(self);
						}, 
						onFail
					);
				},
				onFail
			);
		}
		
	}
}